import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);
// 解决redirect重复路由会警告
// 获取原型对象上的push函数
const originalPush = VueRouter.prototype.push;
// 修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

// 公共路由
export const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login  ",
    meta: { title: "登录" },
    component: () => import("@/views/login.vue"),
  },
  {
    path: "/home",
    name: "home",
    meta: { title: "信贷审核系统" },
    component: () => import("@/layout/index.vue"),
    // redirect: '/index',
    children: [
      {
        path: "/index",
        name: "index",
        meta: { title: "首页", roles: ["input", "approve"], icon: 'el-icon-s-home' },
        component: () => import("@/views/home/home/index.vue"),
      },
    ],
  },
];

//异步路由（都在home的子级）
export const asyncRoutes = [
  {
    path: "/loan-input",
    name: "loan-input",
    meta: { title: "贷款申请", roles: ["input"], icon: 'el-icon-notebook-2' },
    component: () => import("@/views/home/loan-input/index.vue"),
  },
  {
    path: "/input-manager", // 申请管理
    name: "input-manager",
    meta: { title: "申请管理", roles: ["input"], icon: 'el-icon-s-management' },
    component: () => import("@/views/home/input-manager/index.vue"),
  },
  {
    path: "/loan-approve", // 贷款审批
    name: "loan-approve",
    meta: { title: "贷款审批", roles: ["approve"], icon: 'el-icon-s-platform' },
    component: () => import("@/views/home/loan-approve/index.vue"),
    // redirect: '/loan-approve/first',
    // 细节，children path不加/
    children: [
      {
        path: "first", // 初审
        name: "first",
        meta: { title: "初审" },
        component: () => import("@/views/home/loan-approve/first.vue"),
      },
      {
        path: "end", // 终审
        name: "end",
        meta: { title: "终审" },
        component: () => import("@/views/home/loan-approve/end.vue"),
      },
    ],
  },
  {
    path: "/contract", // 标的管理
    name: "contract",
    meta: {
      title: "标的管理",
      roles: ["approve"], icon: 'el-icon-menu'
    },
    component: () => import("../views/home/contract/index.vue"),
  },
  {
    path: "/access-control",
    name: "control",
    meta: {
      title: "权限管理", icon: 'el-icon-s-tools'
    },
    redirect: "/access-control/userList",
    component: () => import("../views/home/access-control/index.vue"),
    children: [
      {
        path: "userList",
        name: "userList",
        meta: { title: "用户展示" },
        component: () => import("../views/home/access-control/userList.vue"),
      },
      {
        path: "createUser",
        name: "createUser",
        meta: { title: "创建管理员" },
        component: () => import("../views/home/access-control/createUser"),
      },
    ],
  },
];

const router = new VueRouter({
  routes,
  mode: "hash"
});

// 防止动态加载路由，路由重复警告
router.$addRoutes = (params) => {
  router.matcher = new VueRouter({ mode: "hash" }).matcher;
  // router.addRoutes(params);
  params.forEach((value) => {
    router.addRoute(value);
  });
};

export default router;
